package com.luo.controller;

import com.alibaba.fastjson.JSON;
import com.luo.model.Order;
import com.luo.model.QueryOrdersVO;
import com.luo.model.ResultObj;
import com.luo.model.Summary;
import com.luo.service.SummaryService;
import com.luo.util.TokenId;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 功能：
 * 作者：Luo。
 * 日期：2024/3/29 9:24
 */
@WebServlet("/member/queryOrders")
public class QueryOrdersController extends HttpServlet {

	SummaryService summaryService = new SummaryService();

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String orderState = req.getParameter("orderState");
		String page = req.getParameter("page");
		String pageSize = req.getParameter("pageSize");
		Object userId = TokenId.getUserId(req, resp);
		if (userId == null){
			return;
		}
		List<Order> orders = summaryService.queryOrders((String) userId,orderState, page, pageSize);
		int count = summaryService.queryOrdersCount((String) userId, orderState, page, pageSize);

		QueryOrdersVO queryOrdersVO = summaryService.queryOrdersResult(orders,count);
		ResultObj resultObj = new ResultObj();
		resultObj.setCode("200");
		resultObj.setMsg("查询成功");
		resultObj.setResult(queryOrdersVO);
		String jsonString = JSON.toJSONString(resultObj);
		resp.getWriter().write(jsonString);
		resp.getWriter().close();
	}
}
